#include <iostream>
#include "tinyxml2.h"

using namespace std;

int createXML(const char* xmlPath)
{
    tinyxml2::XMLDocument doc;
    if(3 != doc.LoadFile(xmlPath))
    {
        cout <<"file has been existed !"<<endl;
        return 0;
    }

    //添加申明可以使用如下两行
    tinyxml2::XMLDeclaration* declaration=doc.NewDeclaration();
    doc.InsertFirstChild(declaration);

    tinyxml2::XMLElement* root=doc.NewElement("XMLUSER");
    doc.InsertEndChild(root);


    tinyxml2::XMLElement* userNode = doc.NewElement("User");
    /*添加属性*/
    userNode->SetAttribute("Name","liangbaikai");
    userNode->SetAttribute("Password ","bbbbbb");
    root->InsertEndChild(userNode);

    return doc.SaveFile(xmlPath);
}

int addXML1(const char* xmlPath)
{
    tinyxml2::XMLDocument doc;
    if(doc.LoadFile(xmlPath) != 0)
    {
        cout<<"load xml file failed"<<endl;
        return false;
    }
    tinyxml2::XMLElement* root=doc.RootElement();
    tinyxml2::XMLElement* userNode=root->FirstChildElement("User");

    tinyxml2::XMLElement* gender = doc.NewElement("Gender");
    tinyxml2::XMLText* genderText=doc.NewText("man");
    gender->InsertFirstChild(genderText);
    userNode->InsertFirstChild(gender);

    tinyxml2::XMLElement* mobile = doc.NewElement("Mobile");
    mobile->InsertFirstChild(doc.NewText("188****6211"));
    userNode->InsertEndChild(mobile);

    tinyxml2::XMLElement* email = doc.NewElement("Email");
    email->InsertFirstChild(doc.NewText("1157***657@qq.com"));
    userNode->InsertEndChild(email);

    tinyxml2::XMLElement* userNode1 = doc.NewElement("User");
    userNode1->SetAttribute("Name","Wenbaikai");
    userNode1->SetAttribute("Password ","aaaaa");
    root->InsertEndChild(userNode1);

    tinyxml2::XMLElement* mobile1 = doc.NewElement("Mobile");
    mobile1->InsertFirstChild(doc.NewText("166****6666"));
    userNode1->InsertEndChild(mobile1);

    return doc.SaveFile(xmlPath);
}

int addXML2(const char* xmlPath)
{
    tinyxml2::XMLDocument doc;
    if(doc.LoadFile(xmlPath) != 0)
    {
        cout<<"load xml file failed"<<endl;
        return false;
    }
    tinyxml2::XMLElement* root=doc.RootElement();
    tinyxml2::XMLElement* userNode=root->FirstChildElement("User");

    tinyxml2::XMLElement*gender = userNode->FirstChildElement("Gender");
    tinyxml2::XMLElement* age = doc.NewElement("Age");
    age->InsertFirstChild(doc.NewText("18"));
    userNode->InsertAfterChild(gender,age);

    tinyxml2::XMLElement*mobile = userNode->FirstChildElement("Mobile");
    mobile->SetAttribute("Location","home");
    tinyxml2::XMLElement* mobile1 = doc.NewElement("Mobile");
    mobile1->SetAttribute("Location","company");
    mobile1->InsertFirstChild(doc.NewText("188****8888"));
    userNode->InsertAfterChild(mobile,mobile1);

    return doc.SaveFile(xmlPath);
}

int deleteXML(const char* xmlPath)
{
    tinyxml2::XMLDocument doc;
    if(doc.LoadFile(xmlPath) != 0)
    {
        cout<<"load xml file failed"<<endl;
        return false;
    }
    tinyxml2::XMLElement* root=doc.RootElement();
    tinyxml2::XMLElement* userNode=root->FirstChildElement("User");
    tinyxml2::XMLElement*mobile = userNode->FirstChildElement("Mobile");
    userNode->DeleteChild(mobile);
    tinyxml2::XMLElement*mobile1 = userNode->FirstChildElement("Mobile");
    mobile1->DeleteAttribute("Location");
    return doc.SaveFile(xmlPath);
}

int changeXML(const char* xmlPath)
{
    tinyxml2::XMLDocument doc;
    if(doc.LoadFile(xmlPath)!=0)
    {
        cout<<"load xml file failed"<<endl;
        return false;
    }
    tinyxml2::XMLElement* root=doc.RootElement();
    tinyxml2::XMLElement* userNode=root->FirstChildElement("User");
    while(userNode != NULL)
    {
        if( 0 == strncmp("liangbaikai",(userNode->Attribute("Name")),11))
        {
            userNode->FirstChildElement("Age")->SetText("10000");
            userNode = userNode->NextSiblingElement();
        }
        else if(( 0 == strncmp("Wenbaikai",(userNode->Attribute("Name")),11)))
        {
            userNode->FirstChildElement("Mobile")->SetText("166****8888");
            userNode = userNode->NextSiblingElement();
        }
        else
        {
            userNode = userNode->NextSiblingElement();
        }
    }
    return doc.SaveFile(xmlPath);
}

int selectXML(const char* xmlPath)
{
    tinyxml2::XMLDocument doc;
    if(doc.LoadFile(xmlPath)!=0)
    {
        cout<<"load xml file failed"<<endl;
        return false;
    }
    tinyxml2::XMLElement* root=doc.RootElement();
    tinyxml2::XMLElement* userNode=root->FirstChildElement("User");
    while(userNode != NULL)
    {
        if( 0 == strncmp("liangbaikai",(userNode->Attribute("Name")),11))
        {
            cout << userNode->Attribute("Name") << endl;
            cout << userNode->Attribute("Password") << endl;
            cout << userNode->FirstChildElement("Age")->GetText() << endl;
            cout << userNode->FirstChildElement("Gender")->GetText() << endl;
            cout << userNode->FirstChildElement("Mobile")->GetText() << endl;
            cout << userNode->FirstChildElement("Email")->GetText() << endl;
            userNode = userNode->NextSiblingElement();
        }
        else
        {
            userNode = userNode->NextSiblingElement();
        }
    }
    return 0;
}



int main()
{
    /*创建*/
    createXML("./bbb.xml");
    /*增*/
    addXML1("./bbb.xml");/*添加性别，号码，邮箱*/ /*再添加一个用户*/
    addXML2("./bbb.xml");/*在性别后面添加年龄，再添加一个号码*/
    /*删*/
    deleteXML("./bbb.xml");/*删除第一个号码，删除第二个号码的属性*/
    /*改*/
    changeXML("./bbb.xml");/*将liangbaikai的年龄改为10000岁，将wenbaikai的号码改为8888结尾*/
    /*查*/
    selectXML("./bbb.xml");/*将liangbaikai的信息打印出来*/

    cout << "Hello World!" << endl;
    return 0;
}
